﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls;

namespace UIT.Events.Keyboard
{
    public class KeyCommandBinding : IDataContext, IContainable<IControl>, IDisposable
    {
        public Keys Key { get; set; }

        public KeyModifiers Modifiers { get { return (KeyModifiers)(Key & Keys.Modifiers); } set { Key = (Key & Keys.KeyCode) | (Keys)value; } }

        public string CommandName { get; set; }

        private object commandParameter;

        public object CommandParameter
        {
            get { return commandParameter; }
            set
            {
                commandParameter = value;
                OnPropertyChanged("CommandParameter");
            }
        }

        public object DataContext
        {
            get
            {
                if (parent == null)
                    return null;
                return Parent.DataContext;
            }
            set { }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        private IControl parent;

        public IControl Parent
        {
            get { return parent; }
            set
            {
                if (parent != null)
                    parent.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(parent_PropertyChanged);
                parent = value;
                if (parent != null)
                    parent.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(parent_PropertyChanged);
                OnPropertyChanged("Parent");
                OnPropertyChanged("DataContext");
            }
        }

        void parent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "DataContext")
                OnPropertyChanged("DataContext");
        }

        private void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(p));
        }


        public void Dispose()
        {
            PropertyChanged = null;
            Parent = null;
        }
    }
}
